/*
 * Created on Feb 18, 2004
 *
 * Author: Sandeep Prakash
 * Project: Sucxentv2 (using leaf nodes only)
 */
package dbsucxentW.storageEdgeW.parserW;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.*;

import javax.sql.rowset.spi.SyncResolver;

import dbsucxentW.storageEdgeW.entitiesW.LeafNode;
import dbsucxentW.storageEdgeW.entitiesW.LeafNodeCollection;

/**
 * @author Administrator
 *
 * 
 */
public class BufferedLeafNodeCollection extends LeafNodeCollection {

	private ConcurrentLinkedQueue _nodes;
	private int _count = 0;
	
	private final static int BUFFER_SIZE = 3500000;
	/**
	 * 
	 */
	public BufferedLeafNodeCollection() {
		super();
		_nodes = new ConcurrentLinkedQueue();
	}

	public void add(LeafNode node) {
		while( _count > BUFFER_SIZE );
		
		_nodes.offer( node );
		
		updateCount( true );
	}

	
	public boolean next() {

		//while( _nodes.isEmpty() && !_isComplete );

		return !_isComplete || !_nodes.isEmpty();
	}

	//new area
	public LeafNode get() {
		
		//while( _nodes.isEmpty() && !_isComplete );
		updateCount( false );
		return ( LeafNode )_nodes.poll();
		
		
	}
	
	private synchronized void updateCount( boolean increase ){
		if( increase ){
			_count++;
		}else{
			_count--;
		}
	}

	
	public LeafNode get(int index) {
		
		return null;
	}
	
	//new area
	public void clear(){
		_nodes.clear();
		_count = 0;
	}

}
